<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="metronic/assets/global/plugins/jquery-1.11.0.min.js" type="text/javascript"></script>
    <link href="metronic/assets/global/plugins/bootstrap/css/bootstrap.min.css" type="text/css"/>
    <link href="metronic/assets/global/css/components.css" rel="stylesheet" type="text/css"/>
    <link href="metronic/assets/admin/pages/css/login.css" rel="stylesheet" type="text/css"/>
</head>
<body class="login">
<div class="content">
    <h3 class="form-title">创建商品</h3>
    <div class="form-group">
        <label class="control-label">商品名</label>
        <div >
            <input class="form-control" type="text" placeholder="商品名" name="title" id="title">
        </div>
    </div>
    <div class="form-group">
        <label class="control-label">商品描述</label>
        <div >
            <input class="form-control" type="text" placeholder="商品描述" name="description" id="description">
        </div>
    </div>
    <div class="form-group">
        <label class="control-label">价格</label>
        <div >
            <input class="form-control" type="number" placeholder="价格" name="price" id="price">
        </div>
    </div>
    <div class="form-group">
        <label class="control-label">图片地址</label>
        <div >
            <input class="form-control" type="text" placeholder="图片地址" name="imgUrl" id="imgUrl">
        </div>
    </div>

    <div class="form-group">
        <label class="control-label">库存</label>
        <div >
            <input class="form-control" type="number" placeholder="库存" name="stock" id="stock">
        </div>
    </div>

    <div class="form-actions">
        <button class="btn blue" id="reg" type="submit">提交商品</button>
    </div>
</div>
<script>
    jQuery(document).ready(function () {

        $("#reg").on("click",function () {
            var title=$("#title").val()
            var description=$("#description").val()
            var price=$("#price").val()
            var imgUrl=$("#imgUrl").val()
            var stock=$("#stock").val()

            if (title == null || title == "") {
                alert("商品名不能为空")
                return false;
            }

            if (description == null || description == "") {
                alert("商品描述不能为空")
                return false;
            }

            if (price == null || price == "") {
                alert("商品价格不能为空")
                return false;
            }

            if (imgUrl == null || imgUrl == "") {
                alert("图片地址不能为空")
                return false;
            }
            if (stock == null || stock == "") {
                alert("库存量不能为空")
                return false;
            }
            $.ajax({
                type:"POST",
                contentType:"application/x-www-form-urlencoded",
                url:"http://localhost:8080/item/addItem",
                data:{
                    "title":title,
                    "description":description,
                    "price":price,
                    "imgUrl":imgUrl,
                    "stock":stock,

                },
                xhrFields:{withCredentials: true},
                success:function (data) {
                    if (data.status == "success") {
                        alert("恭喜"+title
                            +"添加成功")
                        window.location.href="list.html"
                    }else{
                        alert("添加商品失败，原因是"+data.data.errMsg)
                    }
                },
                error:function (data) {  //网络中断时被调用
                    alert("添加商品失败，原因是与服务器网络中断或者"+data.responseText)
                }
            });
        })

    })
</script>
</body>
</html>